/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.model.inventory;

import com.xtd.model.employment.Employee;
import com.xtd.util.CommodityPropertyEnum;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Lu Yuanbin
 */
@Entity
@Table(name = "purchase_record")
@NamedQueries({
    @NamedQuery(name = "PurchaseRecord.findAll", query = "SELECT p FROM PurchaseRecord p")})
public class PurchaseRecord implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "number")
    private String number;
    @Column(name = "name")
    private String name;
    @Column(name = "type")
    private String type;
    @Column(name = "property")
    @Enumerated(EnumType.STRING)
    private CommodityPropertyEnum property;
    @Column(name = "quantity")
    private int quantity;
    @Column(name = "stockin")
    private int stockin;
    @Column(name = "returned_quantity")
    private int returnedQuantity;
    @Column(name = "unit_cost")
    private BigDecimal unitCost;
    @Column(name = "cancelled")
    private boolean cancelled;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "purchaseRecord", fetch = FetchType.EAGER)
    private Set<InventoryRecord> inventoryRecordCollection;
    @JoinColumn(name = "commodity", referencedColumnName = "id")
    @ManyToOne
    private Commodity commodity;
    @JoinColumn(name = "purchase_form", referencedColumnName = "id", insertable = true, updatable = true)
    @ManyToOne
    private PurchaseForm purchaseForm;
    @JoinColumn(name = "createdBy", referencedColumnName = "id")
    @ManyToOne
    private Employee createdBy;
    @Column(name = "createdTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdTime;

    public PurchaseRecord() {
    }

    public PurchaseRecord(PurchaseForm purchaseForm, Commodity commodity, Employee createdBy) {
        this.name = commodity.getName();
        this.number = commodity.getNumber();
        this.quantity = 1;
        this.commodity = commodity;
        this.property = commodity.getProperty();
        this.type = commodity.getType();
        this.purchaseForm = purchaseForm;
        this.createdBy = createdBy;
        this.createdTime = new Date();
        this.unitCost = commodity.getUnitCost();
    }

    public PurchaseRecord(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public BigDecimal getUnitCost() {
        return unitCost;
    }

    public void setUnitCost(BigDecimal unitCost) {
        this.unitCost = unitCost;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PurchaseRecord)) {
            return false;
        }
        PurchaseRecord other = (PurchaseRecord) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.xtd.model.inventory.PurchaseRecord[id=" + id + "]";
    }

    /**
     * @return the inventoryRecordCollection
     */
    public Set<InventoryRecord> getInventoryRecordCollection() {
        return inventoryRecordCollection;
    }

    /**
     * @param inventoryRecordCollection the inventoryRecordCollection to set
     */
    public void setInventoryRecordCollection(Set<InventoryRecord> inventoryRecordCollection) {
        this.setInventoryRecordCollection(inventoryRecordCollection);
    }

    /**
     * @return the cancelled
     */
    public boolean isCancelled() {
        return cancelled;
    }

    /**
     * @param cancelled the cancelled to set
     */
    public void setCancelled(boolean cancelled) {
        this.cancelled = cancelled;
    }

    /**
     * @return the commodity
     */
    public Commodity getCommodity() {
        return commodity;
    }

    /**
     * @param commodity the commodity to set
     */
    public void setCommodity(Commodity commodity) {
        this.commodity = commodity;
    }

    /**
     * @return the stockin
     */
    public Integer getStockin() {
        return stockin;
    }

    /**
     * @return the returnedQuantity
     */
    public int getReturnedQuantity() {
        return returnedQuantity;
    }

    /**
     * @param returnedQuantity the returnedQuantity to set
     */
    public void setReturnedQuantity(int returnedQuantity) {
        this.returnedQuantity = returnedQuantity;
    }

    /**
     * @return the number
     */
    public String getNumber() {
        return number;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(String number) {
        this.number = number;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the property
     */
    public CommodityPropertyEnum getProperty() {
        return property;
    }

    /**
     * @param property the property to set
     */
    public void setProperty(CommodityPropertyEnum property) {
        this.property = property;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /**
     * @param stockin the stockin to set
     */
    public void setStockin(int stockin) {
        this.stockin = stockin;
    }

    /**
     * @return the purchaseForm
     */
    public PurchaseForm getPurchaseForm() {
        return purchaseForm;
    }

    /**
     * @param purchaseForm the purchaseForm to set
     */
    public void setPurchaseForm(PurchaseForm purchaseForm) {
        this.purchaseForm = purchaseForm;
    }

    /**
     * @return the createdBy
     */
    public Employee getCreatedBy() {
        return createdBy;
    }

    /**
     * @param createdBy the createdBy to set
     */
    public void setCreatedBy(Employee createdBy) {
        this.createdBy = createdBy;
    }

    /**
     * @return the createdTime
     */
    public Date getCreatedTime() {
        return createdTime;
    }

    /**
     * @param createdTime the createdTime to set
     */
    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }
    /**
     * @param inventoryRecordCollection the inventoryRecordCollection to set
     */
}
